package com.njupt.webapp.tags;

import javax.servlet.jsp.JspException;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import com.njupt.dao.BaseDao;

/**
 * 输入hql语句，执行改语句，存储查询结果。
 *
 */
public class HqlQueryForUniqueTag extends HqlBaseTag{
	public int doStartTag() throws JspException {
        String hqlx = (String) TagUtils.evaluate("hql",hql, String.class,this,pageContext);
        BaseDao baseDao = this.getBaseDao();
      //判断是否使用cache
        if( super.useCache ){
        	Cache cache = this.getCache();
            Element element = cache.get(hqlx);
          //如果cache中命中
            if( null != element ){
            	TagUtils.saveToContext(this,pageContext,scope, element.getObjectValue());
            }else{
            	//如果不命中
            	Object o = baseDao.queryForUnique(hqlx,null);
            	//放入cache
            	element = new Element(hqlx,o);
	        	cache.put(element);
            	TagUtils.saveToContext(this,pageContext,scope,o);
            }
        }else{
        	//不使用cache
        	Object o = baseDao.queryForUnique(hqlx,null);
        	TagUtils.saveToContext(this,pageContext,scope,o);
        }

        return super.doStartTag();
  
	}
}
